<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @cancel="handleCancel"
    :footer="null"
    cancelText="关闭">

    <div>
      <a-form-model ref="form" :model="model" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="客户名字" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.customerInfo.name" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="项目地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="model.customerInfo.address" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="下单时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-model="model.placeOrderTime"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="送达时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-model="model.deliveredTime"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
              <a-select
                show-search
                placeholder="请选择供应商"
                option-filter-prop="children"
                v-model="model.supplierId"
                style="width: 100%"
                :filter-option="filterOptionSupplier"
                allowClear
                @change="changeSupplier">
                <a-select-option :value="item.id" v-for="(item,index) in supplierData" :key="index">
                  {{item.supplierName}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="操作" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-button type="primary" style="margin-right: 10px" :loading="loading" @click="createOrder(1)">生成采购订单</a-button>
              <a-button type="primary" :loading="loading" @click="createOrder(2)">生成出库订单</a-button>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>

    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{y:500}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{
          selectedRowKeys: selectedRowKeys,
          onSelectAll:onSelectAll,
          onSelect:onSelect,
          onChange: onSelectChange,
          type:'checkbox'
        }"
        class="j-table-force-nowrap">

        <span slot="action" slot-scope="text, record">
            <a @click="placeOrderDetail(record.id)">详情</a>
            <a-divider type="vertical"/>
            <a-popconfirm title="当前订单材料是否已送达?" @confirm="() => confirmDelivered(record.id)">
              <a>确认送达</a>
            </a-popconfirm>
        </span>
      </a-table>
    </div>
  </a-modal>
</template>

<script>
  import { filterObj } from '@/utils/util'
  import { getAction, postAction } from '@/api/manage'
  export default {
    name: "placeOrderMaterialDetailModal",
    data() {
      return {
        width: '90%',
        model: {},
        title: "",
        columns:[
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            },
            fixed: 'left'
          },
          {
            title:'名称',
            align:"center",
            dataIndex: 'material.materialName',
            width: "15%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '15%',
                }
              }
            }
          },
          {
            title:'型号',
            align:"center",
            dataIndex: 'material.modelNumber',
            width: "8%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '8%',
                }
              }
            }
          },
          {
            title:'规格',
            align:"center",
            dataIndex: 'material.specifications',
            width: "8%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '8%',
                }
              }
            }
          },
          {
            title:'品牌',
            align:"center",
            dataIndex: 'material.brand',
            width: "8%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '8%',
                }
              }
            }
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'material.unit',
            width: "5%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '5%',
                }
              }
            }
          },
          {
            title:'供应商',
            align:"center",
            dataIndex: 'supplierName',
            width: "15%",
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '15%',
                }
              }
            }
          },
          {
            title:'单价/元',
            align:"center",
            dataIndex: 'unitPrice',
            customRender:function (text) {
              if (text == "" || text == null){
                return "-"
              }else {
                return text
              }
            }
          },
          {
            title:'预算数量',
            align:"center",
            dataIndex: 'quantity',
            customRender:function (text) {
              if (text == null){
                return "-"
              } else {
                return text
              }
            }
          },
          {
            title:'本次下单数量',
            align:"center",
            dataIndex: 'thisOrderQuantity',
            customRender:function (text) {
              if (text == null){
                return "-"
              } else {
                return text
              }
            }
          },
          {
            title:'待下单数量',
            align:"center",
            dataIndex: 'expectQuantity',
            customRender:function (text) {
              if (text == null){
                return "-"
              } else {
                return text
              }
            }
          },
          {
            title:'小计/元',
            align:"center",
            dataIndex: 'subtotal',
            customRender:function (text) {
              if (text == "" || text == null){
                return "-"
              } else {
                return text
              }
            }
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            width: '8%',
            customRender:function (text) {
              if (text == 0){
                return "未生成订单"
              } else if (text == 1){
                return "已生成采购订单"
              } else if (text == 2){
                return "已生成出库订单"
              }
            },
            customCell:() => {
              return {
                style: {
                  wordWrap:'break-word',
                  wordBreak:'break-all',
                  whiteSpace:'normal',
                  minHeight:'50px',
                  width: '8%',
                }
              }
            }
          },
          {
            title:'库存',
            align:"center",
            dataIndex: 'stock',
          },
        ],
        visible: false,
        // 查询条件
        queryParam: {},
        //数据集
        dataSource: [],
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
        ipagination:{
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        labelCol: {
          xs: {span: 24},
          sm: {span: 6},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        url: {
          placeOrderItemList: "/ghProjectPlaceOrderItem/list",
          querySupplierList: "/ghSupplychainSupplier/allList",
          purchaseOrderAddUrl: '/ghProjectPlaceOrderItem/purchaseOrderAdd',
          outDepositoryAddUrl: '/ghProjectPlaceOrderItem/outDepositoryAdd',
        },
        currentPlaceOrderId: 0,
        supplierData: [],
      }
    },
    created() {
      this.loadSupplierData();
    },
    methods: {
      handleCancel() {
        this.visible = false;
        this.onClearSelected();
      },
      loadData(arg) {
        this.loading = true;
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        let params = {};
        params.placeOrderId = this.currentPlaceOrderId
        params.supplierId = this.currentSupplierId
        getAction(this.url.placeOrderItemList,params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result;
            if (res.result.total) {
              this.ipagination.total = res.result.total;
            } else {
              this.ipagination.total = 0;
            }
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectedRows = [];
      },

      show(record){
        this.model = record;
        this.currentPlaceOrderId = record.id;
        this.visible = true;
        this.title = "材料详情";
        this.loadData(1);
      },
      onSelectAll(selected, selectedRows, changeRows) {
        if(selected) {
          let rows = [...selectedRows]
          this.selectedRows = this.selectedRows.concat(rows)
        } else {
          changeRows.forEach(item => {
            const delIndex = this.selectedRows.findIndex((val) => {
              return val.id === item.id
            })
            this.selectedRows.splice(delIndex, 1)
          })
        }
      },
      onSelect(record, selected) {
        if(selected) {
          this.selectedRows.push(record);
        } else {
          const delIndex = this.selectedRows.findIndex((val) => {
            return val.id === record.id
          })
          this.selectedRows.splice(delIndex,1)
        }
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
      //生成订单
      createOrder(value){
        const that = this;
        if (value === 1){
          if (this.model.supplierId == null){
            this.$message.warning("请先选择供应商")
            return;
          }
        }
        if (this.selectedRows.length === 0){
          this.$message.warning("请至少选择一条数据")
          return;
        }
        this.model.placeOrderItemList = this.selectedRows;
        let url = "";
        if (value === 1){
          url = this.url.purchaseOrderAddUrl;
        }else if (value === 2){
          url = this.outDepositoryAddUrl;
        }
        this.loading = true;
        postAction(url, this.model).then((res) => {
          if (res.success) {
            that.loadData(1);
            that.$message.success(res.message)
          } else {
            that.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //加载供应商
      loadSupplierData(){
        getAction(this.url.querySupplierList).then((res) => {
          if (res.success) {
            this.supplierData = res.result.records||res.result;
          }else{
            this.$message.warning(res.message)
          }
        })
      },
      filterOptionSupplier(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeSupplier(value) {
        this.currentSupplierId = value;
        this.loadData(1);
      },
    }
  }
</script>

<style scoped>
  .ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
</style>